package com.fooddelivery.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fooddelivery.Common.Result;
import com.fooddelivery.Common.queryParam.QueryPageParamDish;
import com.fooddelivery.Entity.Dish;
import com.fooddelivery.Service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 菜品 前端控制器
 * </p>
 *
 * @author zhengruiyang
 * @since 2025-08-31
 */
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    DishService dishService;

    @RequestMapping("/dishCount")
    public Result dishCount() {
        return Result.suc(dishService.dishCount());
    }
    @RequestMapping("/list")
    public Result list() {
        return Result.suc(dishService.list());
    }
    @PostMapping("/updateAll")
    public Result updateAll(@RequestBody Dish dish) {
         dishService.updateAll(dish);
        return Result.sucMsg("修改成功");
    }
    @RequestMapping("/updateStatus")
    public Result updateStatus(@RequestBody Dish dish) {
         dishService.updateStatus(dish);
         return Result.sucMsg("修改成功");
    }
    @RequestMapping("/save")
    public Result save(@RequestBody Dish dish) {
         dishService.save(dish);
         return Result.sucMsg("添加成功");
    }
    @RequestMapping("/getById")
    public Result getById(@RequestBody Dish dish) {//这里也可以直接写ID，不必传入Dish对象
         return Result.sucMsg("查询成功",dishService.getById(dish.getId()));
    }
    @DeleteMapping("/delete")
    public Result delete(@RequestBody Dish dish) {//这里也可以直接写ID，不必传入Dish对象
         dishService.removeById(dish.getId());
         return Result.sucMsg("删除成功");
    }
    @RequestMapping("/listPageDish")
    //@RequestBody 作用：将json数据映射为对象
    //或者直接用map接收
    //分页查询
    public Result listPage(@RequestBody QueryPageParamDish queryPageParamDish){
        Page<Dish> page = new Page();
        page.setCurrent(queryPageParamDish.getPageNum());
        page.setSize(queryPageParamDish.getPageSize());
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Dish dish = queryPageParamDish.getDish(); // 获取查询条件对象
        if (StringUtils.hasText(dish.getName())) {
            lambdaQueryWrapper.like(Dish ::getName, dish.getName());
        }
        if(dish.getCategoryId()!= null&&dish.getCategoryId()!=0){
            lambdaQueryWrapper.eq(Dish ::getCategoryId, dish.getCategoryId());
        }
        IPage result=dishService.page(page,lambdaQueryWrapper);
        System.out.println(result.getTotal());
        System.out.println(result.getRecords().size());
        return Result.suc(result.getRecords());
    }
}
